# -*- coding: utf-8 -*-

import requests, re
from urllib import parse
import os
from multiprocessing import Pool
import pymongo
from fake_useragent import UserAgent
import time

host = 'localhost'
data_base = 'Lianjia'
client = pymongo.MongoClient(host)
t_xiaoqu = 'xiaoqu'
db = client[data_base]
base_dir = './long_lat/'
ua = UserAgent()
headers1 = {'User-Agent': 'ua.ramdom'}


def query(xiaoqu_info):
    try:
        # file_path = base_dir + xiaoqu_info['小区编号'] + '.txt'
        # if os.path.exists(file_path):
        #     return
        url = 'http://apis.map.qq.com/jsapi?'
        data = {
            'qt': 'poi',
            'wd': '北京市 ' + xiaoqu_info['小区名称'] + ' ' + xiaoqu_info['小区别称'],
            'pn': 0,
            'rn': 10,
            'rich_source': 'qipao',
            'rich': 'web',
            'nj': 0,
            'c': 1,
            'key': 'FBOBZ-VODWU-C7SVF-B2BDI-UK3JE-YBFUS',
            'output': 'json',
            'pf': 'jsapi',
            'ref': 'jsapi',
            'cb': 'qq.maps._svcb3.search_service_0'}
        coordinate_url = url + parse.urlencode(data)
        r = requests.get(coordinate_url, headers=headers1)
        if '"status": 140,' in r.text or '"status": 141' in r.text:
            time.sleep(3)
            query(xiaoqu_info)
            return
        if not os.path.exists(base_dir):
            os.mkdir(base_dir)
        with open(base_dir + xiaoqu_info['小区编号'] + '.txt', 'w+') as f:
            f.write(r.text)
            print(xiaoqu_info['小区编号'])
    except:
        pass


if __name__ == '__main__':
    with Pool(os.cpu_count()) as p:
        p.map(query, [xiaoqu_info for xiaoqu_info in db[t_xiaoqu].find().batch_size(50) if
                      'longitude' not in xiaoqu_info or xiaoqu_info['longitude'] is None])
